package com.yunchao.feature.base.presentation.activity

import android.os.Bundle
import android.view.View
import by.kirich1409.viewbindingdelegate.viewBinding
import com.chad.library.adapter4.BaseQuickAdapter
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import com.xuexiang.xui.utils.WidgetUtils
import com.yunchao.feature.base.R
import com.yunchao.feature.base.databinding.FragmentCommonRefreshLayoutBinding
import com.yunchao.feature.base.domain.model.PageInfo
import timber.log.Timber

abstract class BaseRefreshFragment<Data : Any, Adapter : BaseQuickAdapter<Data, *>> :
    BaseFragment(R.layout.fragment_common_refresh_layout) {

    protected val binding: FragmentCommonRefreshLayoutBinding by viewBinding()
    protected val mListAdapter: Adapter by lazy { createAdapter() }

    abstract fun createAdapter(): Adapter
    abstract fun loadData(pageNum: Int)

    protected val pageInfo: PageInfo = PageInfo()

    override fun initView(view: View, savedInstanceState: Bundle?) {
        binding.rvList.apply {
            adapter = mListAdapter
            WidgetUtils.initRecyclerView(this, 0)
        }

        binding.refreshLayout.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onRefresh(refreshLayout: RefreshLayout) {
                refresh()
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                loadMore()
            }

        })
        binding.refreshLayout.autoRefresh()
    }

    /**
     * 刷新数据
     */
    private fun refresh() {
        pageInfo.reset()
        loadData(pageInfo.page)
    }


    /**
     * 加载更多数据
     */
    private fun loadMore() {
        loadData(pageInfo.page)
    }

    /**
     * 开始加载数据，显示加载中状态
     */
    protected fun loading() {

    }

    /**
     * 数据加载成功处理
     *
     * @param list 加载成功的数据列表
     */
    protected fun loadSuccess(total: Int, list: List<Data>) {
        if (pageInfo.isFirstPage) {
            mListAdapter.submitList(list.toMutableList())
        } else {
            mListAdapter.addAll(list)
        }.apply {
            if ((pageInfo.page * PageInfo.PAGE_SIZE) >= total) {
                /**
                 *Set the status to not loaded, and there is no paging data.
                 *设置状态为未加载，并且没有分页数据了
                 */
                binding.refreshLayout.finishLoadMoreWithNoMoreData()
            } else {
                /**
                 * Set the state to not loaded, and there is also paginated data
                 *设置状态为未加载，并且还有分页数据
                 */
                binding.refreshLayout.finishRefresh()
                binding.refreshLayout.finishLoadMore()
                pageInfo.nextPage()
            }
        }

    }

    /**
     * 数据加载失败处理
     *
     * @param errorMsg 错误信息
     */
    protected fun loadFail(errorMsg: String) {
        Timber.i("loadFail:$errorMsg")
        binding.refreshLayout.finishRefresh(false)
        binding.refreshLayout.finishLoadMore(false)
    }
}